﻿function LookupArgsClass() {
    // public:
    //
    this.items = null;

}

//	功能：打开查询窗口并设置返回值
//	lookupField: optional field to bind values
//	lookupStyle: multi/single/subject
//	lookupClass: file name in /lookup/lookupxml - minus ext.
//	lookupTypes: CSV of object types (1,2,9)
//	lookupBrowse: 1/0 -- browse or search
//
//function LookupObjects(lookupField, lookupStyle, lookupClass, lookupTypes, lookupBrowse, bindingColumns, additionalParams, showNew, showProp)
function LookupObjects(lookupField, lookupStyle, lookupClass, lookupBrowse, bindingColumns, additionalParams, lookupCustom, features) {
    var url;

    if (lookupCustom == null || lookupCustom == "") {
        url = "/Lookup/map_lookup";
        url += lookupStyle;
        url += ".aspx";
        url += "?class=" + lookupClass;
        //url += "&objecttypes=" + lookupTypes;
        url += "&browse=" + lookupBrowse;

        if (bindingColumns) {
            url += "&bindingcolumns=" + bindingColumns;
        }

        if (additionalParams) {
            if (additionalParams.charAt(0) != "&") {
                url += "&";
            }
            url += additionalParams;
        }
       
        /*		
        if (showNew)
        {
        url += "&ShowNewButton=" + showNew;
        }

		if (showProp)
        {
        url += "&ShowPropButton=" + showProp;
        }
        */
    }
    else {
        url = lookupCustom;
    }

    var args = new LookupArgsClass();
    if (lookupField != null) {
        args.items = lookupField.getElementsByTagName("SPAN");
    }

    //var features = BuildFeatures(lookupStyle);
    if (features == null)
        features = BuildFeatures(lookupStyle);

    if (features == null)
        return;
     
    var lookupItems = window.showModalDialog(url, args, features);

    if (lookupItems != null && lookupField != null) {
        BuildField(lookupField, lookupItems); 		//将返回值显示在界面上
    }

    return lookupItems;
}


function BuildFieldSpan(lookupField, lookupItems) {

    var html = "";
    var len = lookupItems.items.length;
    for (var i = 0; i < len; ++i) {
        var item = lookupItems.items[i];

        html += (i > 0 ? " " : "");

        var e = parent.document.createElement("SPAN");

        e.className = "lui";
        e.oid = item.id;
        //e.otype		= item.type;
        e.innerHTML = item.html;
        e.onclick = "openlui();";

        html += e.outerHTML;
    }
    if (html.length == 0) {
        html = "&nbsp;";
    }
    return html;
}

function BuildField(lookupField, lookupItems) {
    lookupField.innerHTML = BuildFieldSpan(lookupField, lookupItems);
}


function BuildFeatures(lookupStyle) {
    var height;
    var width;

    switch (lookupStyle) {
        case "multi":
            height = "460px";
            width = "520px";
            break;
        case "single":
            height = "488px";
            width = "498px";
            break;
        case "subject":
            height = "450px";
            width = "500px";
            break;
        default:
            alert("未知的 Lookup 样式：" + lookupStyle);
            return null;
    }

    return "dialogHeight:" + height + " ;dialogWidth:" + width + ";resizable:yes;center:yes;status:no;help:no;scroll:no;";
}

//	open a lookup item
//
function openlui() {
    
    var o = event.srcElement;
     
    while (o.tagName != "SPAN") {
        o = o.parentElement;
    }
    var id = o.parentElement.parentElement.nextSibling.childNodes[1].value;
     
    try {
        if (!o.parentElement.openLui(o)) return;
        
        window.open(o.parentElement.detailpage + id, "", "width=750,height=510,status=1,resizable=1");
    }
    catch (e) { }

    /*
    if (o.otype == UnresolvedAddress)		//UnresolvedAddress  在global.js中定义
    {
    resolveAddress(o.otype, o.oid);
    }
    else
    {
    openObj(o.otype, o.oid);
    }
    */
}

function ClearField(dataInputs) {
    for (var di = 0; di < dataInputs.length; ++di) {
        dataInputs[di].value = "";
    }
}

function BindField(lookupItems, dataInputs) {
    ClearField(dataInputs);

    var html = "";
    var len = lookupItems.items.length;

    for (var i = 0; i < len; ++i) {
        var item = lookupItems.items[i];

        //	look through the data inputs which are bound to this
        //	lookup and set the id where the type matches the input type		
        //
        for (var di = 0; di < dataInputs.length; ++di) {
            var input = dataInputs[di];

            if (input.otype == item.type) {
                input.value = item.id;
            }
            else {
                input.value = "";
            }
        }
    }
}
